package com.holystone.splunk.app.common;

import com.holystone.splunk.app.model.AlertVO;
import com.holystone.splunk.app.mybatis.model.Event;
import com.holystone.splunk.app.service.IIndexAlertService;
import com.splunk.FiredAlert;
import com.splunk.FiredAlertGroup;
import com.splunk.Service;
import org.apache.log4j.Logger;

import java.util.Collection;
import java.util.Set;

/**
 * Copyright (c) 2017, HOLYSTONE Technologies, Inc.
 * All right reserved.
 *
 * @author huyan
 * @date 2017/11/10
 * @desc: 同源操作时，在主线程中无法获取到最新的告警，因此开辟一条子进程来处理保存操作
 */
public class AlertRunnable implements Runnable {

    private final Logger logger = Logger.getLogger(this.getClass());
    // splunk service
    private Service service;
    // 保存新告警的service
    private IIndexAlertService indexAlertService;
    // 新告警名称
    private String alertName;
    // 新告警sid
    private String newAlertSid;
    // 旧告警sid
    private String oldAlertSid;

    // 线程结束标志
    private Boolean isAlive = true;

    public AlertRunnable(Service service, IIndexAlertService indexAlertService, String newAlertSid, String alertName, String oldAlertSid) {
        this.service = service;
        this.indexAlertService = indexAlertService;
        this.newAlertSid = newAlertSid;
        this.alertName = alertName;
        this.oldAlertSid = oldAlertSid;
    }

    @Override
    public void run() {
        logger.debug("启动子线程查询新告警");
        FiredAlertGroup group = null;
        while (isAlive) {
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            group = service.getFiredAlertGroups().get(alertName);
            if (group == null) {
                logger.error("告警group为null");
                continue;
            }
            Collection<FiredAlert> alerts = group.getAlerts().values();
            for (FiredAlert alert : alerts) {
                if (alert.getSid() != null && alert.getSid().equals(newAlertSid)) {
                    logger.debug("查询到新告警");
                    isAlive = false;
                    try {
                        // 保存新告警
                        Set<Event> events = null;
                        if (oldAlertSid != null && oldAlertSid != "") {
                            AlertVO alertVO = indexAlertService.getAlertsBySids(oldAlertSid).get(0);
                            if (alertVO != null) {
                                events = alertVO.getEvents();
                            }
                        }
                        indexAlertService.saveAlertFromFiredAlert(alert, events);
                        break;
                    } catch (Exception e) {
                        logger.error("查询告警出错：" + e.getMessage(), e);
                        e.printStackTrace();
                    }
                }
            }
            if (isAlive) {
                logger.debug("查询不到新告警");
            }
        }
    }

    public void setAlive(Boolean alive) {
        isAlive = alive;
    }
}
